#include <stdio.h>
#include <string.h>
#include "file.h"

int main()
{
    int a;
    struct contt con;
    text_init(&con);
    do
    {
        printf("\n");
        printf("********************************\n");
        printf("******1 add       2 del    *****\n");
        printf("******3 change    4 reach  *****\n");
        printf("******5 print     0 exit   *****\n");
        printf("***please choose you function***\n");
        printf("********************************\n");
        printf("\n");
        scanf("%d",&a);
        switch(a)
        {
            case 1:
            text_add(&con);
                break;
                case 2:
                text_del(&con);
                break;
                case 5:
                text_show(&con);
                break;
                case 4:
                text_reach(&con);
                break;
                case 3:
                text_change(&con);
                break;

                
        }

    }while(a);
}


void text_init(struct contt* pc){
    memset(pc->data,0,sizeof(pc->data));
    pc->size=0;
}

int text_add(struct contt *pc)
{
    if(pc->size>=MAX)
    {
        printf("can not add:full");
        return -1;
    }

    printf("name:");
    scanf("%s",pc->data[pc->size].name);
    printf("age:");
    scanf("%d",&(pc->data[pc->size].age));
    printf("sex:");
    scanf("%s",pc->data[pc->size].sex);
    printf("tele:");
    scanf("%s",pc->data[pc->size].tele);

    pc->size++;
    printf("add success\n");
    return 0;
}

void text_show(const struct contt *pc)
{
    if(pc->size==0)
    {
        printf("is NULL\n");
        return;
    }
    printf("%-10s\t%-6s\t%-5s\t%-15s\n","name","age","sex","tele");
    for(int i=0;i<pc->size;i++)
    {
        printf("[%d]%-10s\t%-6d\t%-5s\t%-15s\n",i+1,pc->data[i].name,pc->data[i].age,pc->data[i].sex,pc->data[i].tele);
        printf("\n");
    }
}

// void text_del(struct contt *pc)//编号删除
// {
//     int pos;
//     printf("please tell me delete pos:");
//     scanf("%d",&pos);
//     if(pos<0||pos>pc->size)
//     {
//         printf("delete failue\n");
//         return ;
//     }
//     for(int i=pos-1;i<pc->size;i++)
//     {
//         pc->data[i]=pc->data[i+1];
//     }
//     pc->size--;
// }

void text_del(struct contt *pc)   //名字删除
{
    int add;
    add=Find_pos(pc);
    if(add<pc->size&&(add!=-1))
    {
        for(int a=add;a<pc->size-1;a++)
        {
            pc->data[a]=pc->data[a+1];
        }
        pc->size--;
        printf("delete success\n");
    }
    else
    {
        printf("delete failue\n");
    }
    
}


void text_reach(const struct contt *pc)
{
    int add;
    add=Find_pos(pc);
    if(add<pc->size&&(add!=-1))
    {
        printf("\n");
        printf("%-10s\t%-6s\t%-5s\t%-15s\n","name","age","sex","tele");
        printf("[%d]%-10s\t%-6d\t%-5s\t%-15s\n",add+1,pc->data[add].name,pc->data[add].age,pc->data[add].sex,pc->data[add].tele);
        printf("reach success\n");
    }
    else{
        printf("reach failue\n");
    }
}

int Find_pos(const struct contt *pc)
{
    int i;
    char name[Max_name];
    printf("please tell me the name: ");
    scanf("%s",name);
    for(i=0;i<pc->size;i++)
    {
        if(strcmp(name,pc->data[i].name)==0)
        return i;
    }
        return -1;
}

void text_change(struct contt *pc)
{
    int add;
    int i;
    add = Find_pos(pc);
    if(add==-1)
    {
        printf("name is not zai\n");
        return;
    }
    printf("please tell me the message of you want: 1.name 2.age 3.sex 4.tele :  ");
    scanf("%d",&i);
    switch(i)
    {
        case 1:
        printf("please tell me the name of you want: ");
        scanf("%s",pc->data[add].name); 
        printf("change name success \n");
        break; 
        case 2:
        printf("please tell me the age of you want: ");
        scanf("%d",&(pc->data[add].age)); 
        printf("change age success \n");
        break; 
        case 3:
        printf("please tell me the sex of you want: ");
        scanf("%s",pc->data[add].sex); 
        printf("change sex success \n");
        break;             
        case 4:
        printf("please tell me the tele of you want: ");
        scanf("%s",pc->data[add].tele); 
        printf("change tele success \n");
        break;                                                       
    }
}